package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.constant.NumConstant;
import com.example.entity.Spu;
import com.example.entity.buyer.UserCollect;
import com.example.entity.enums.UserPreferEnum;
import com.example.exception.BaseException;
import com.example.mapper.SkuMapper;
import com.example.mapper.SpuMapper;
import com.example.mapper.UserCollectMapper;
import com.example.service.UserCollectService;
import com.example.vo.IdHolder;
import com.example.vo.PageVo;
import com.example.vo.buyer.CollectSpuVo;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Description:
 * @Author: aimi
 * @Create: 2024-05-22
 **/
@Service
public class UserCollectServiceImpl implements UserCollectService {
    @Autowired
    private UserCollectMapper collectMapper;
    @Resource
    private SpuMapper spuMapper;
    @Resource
    private SkuMapper skuMapper;
    @Resource
    private UserPreferService userPreferService;

    @Override
    public void collectSpu(Long spuId,Integer operation) {
        Long userId = IdHolder.getUserId();
        QueryWrapper<UserCollect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).eq("spu_id", spuId);
        Long count = collectMapper.selectCount(queryWrapper);
        //收藏
        if(operation == NumConstant.OPERATION_COLLECT){
            if (count != 0) {
                throw new BaseException("你已收藏该商品");
            }
            userPreferService.recordUserScore(userId,spuId, UserPreferEnum.COLLECT);

            UserCollect userCollect = new UserCollect();
            userCollect.setSpuId(spuId);
            userCollect.setType(1);
            userCollect.setCreateTime(LocalDateTime.now());
            userCollect.setUserId(userId);
            collectMapper.insert(userCollect);
        }
        //取消收藏
        else if(operation == NumConstant.OPERATION_CANCEL){
            if(count == 0){
                throw new BaseException("你没有收藏该商品");
            }
            collectMapper.delete(queryWrapper);
        }
    }


    @Override
    public boolean getCollectSpuStatus(Long spuId) {
        Long userId = IdHolder.getUserId();
        if(userId == null){
            return false;
        }
        QueryWrapper<UserCollect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId).eq("spu_id",spuId);
        Long count = collectMapper.selectCount(queryWrapper);
        return count == 1;
    }

    @Override
    public PageVo getSpuCollectPage(Integer pageNo, Integer pageSize) {
        Long userId = IdHolder.getUserId();
        int begin = (pageNo-1)*pageSize;
        List<Long> spuIds = collectMapper.getSpuIds(userId,begin,pageSize);
        if(spuIds == null || spuIds.isEmpty()){
            return new PageVo(Collections.EMPTY_LIST,0L);
        }
        List<Spu> spuList = spuMapper.selectBatchIds(spuIds);
        ArrayList<CollectSpuVo> list = new ArrayList<>();
        spuList.forEach(spu -> {
            Long spuId = spu.getId();
            CollectSpuVo collectSpuVo = new CollectSpuVo();
            collectSpuVo.setSpuId(spuId);
            collectSpuVo.setName(spu.getName());
            String defaultImage = skuMapper.getDefaultImageBySpuId(Integer.parseInt(spuId.toString()));
            collectSpuVo.setDefaultImage(defaultImage);
            list.add(collectSpuVo);
        });
        QueryWrapper<UserCollect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId).eq("type",1);
        Long total = collectMapper.selectCount(queryWrapper);
        return new PageVo(list,total);
    }

    @Override
    public PageVo getStoreCollectPage(Integer pageNo, Integer pageSize) {
        return new PageVo(new ArrayList<>(),0L);
    }

    @Override
    public void collectStore(Long storeId, Integer operation) {
        Long userId = IdHolder.getUserId();
        QueryWrapper<UserCollect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).eq("store_id", storeId);
        Long count = collectMapper.selectCount(queryWrapper);
        //收藏
        if(operation == NumConstant.OPERATION_COLLECT){
            if (count != 0) {
                throw new BaseException("你已收藏该店铺");
            }
            UserCollect userCollect = new UserCollect();
            userCollect.setStoreId(storeId);
            userCollect.setType(2);
            userCollect.setCreateTime(LocalDateTime.now());
            userCollect.setUserId(userId);
            collectMapper.insert(userCollect);
        }
        //取消收藏
        else if(operation == NumConstant.OPERATION_CANCEL){
            if(count == 0){
                throw new BaseException("你没有收藏该商品");
            }
            collectMapper.delete(queryWrapper);
        }
    }

    @Override
    public boolean getCollectStoreStatus(Long storeId) {
        Long userId = IdHolder.getUserId();
        if(userId == null){
            return false;
        }
        QueryWrapper<UserCollect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId).eq("store_id",storeId);
        Long count = collectMapper.selectCount(queryWrapper);
        return count == 1;
    }
}